package com.hehe.service;

import com.hehe.entity.*;
import com.hehe.mapper.GoodsCollectionMapper;
import com.hehe.mapper.GoodsInfoMapper;
import com.hehe.mapper.GoodsTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService{
    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private GoodsCollectionMapper goodsCollectionMapper;
    /**
     * 商品种类
     * @param goodsType
     */
    public int add_type(GoodsType goodsType) {
       int result =0;
       try {
           result =  goodsTypeMapper.add_type(goodsType);
       }catch (Exception e){
           System.out.println("添加失败");
           return 0;
       }

       if (result>0) {
           System.out.println("添加成功");
       }
       return result;
    }

    public int del_type(GoodsType goodsType) {
        int result =0;
        try {
            result =  goodsTypeMapper.del_type(goodsType);
        }catch (Exception e){
            System.out.println("删除失败");
        }

        if (result>0) {
            System.out.println("删除成功");
        }
        return result;
    }

    public  int update_name(GoodsType goodsType){
        return goodsTypeMapper.update_name(goodsType);
    }

    public GoodsType findOneById_type(int Id) {
        GoodsType type = goodsTypeMapper.findOneById_type(Id);

        return type;
    }

    public List<GoodsType> findAll_type() {
        List<GoodsType> lists = goodsTypeMapper.findAll_type();
        return lists;
    }

    /**
     * 商品详细信息
     * @param goodsInfo
     */
    public int add_goods(GoodsInfo_Table goodsInfo) {

        int  result =  goodsInfoMapper.add_goods(goodsInfo);
        if (result>0) System.out.println("添加成功");
        return result;

    }

    //删除商品
    public int del_goods(int goods_id) {
        int  result =  goodsInfoMapper.del_goods(goods_id);
        if (result>0) System.out.println("删除成功");
        return result;
    }

    public int update_goods(GoodsInfo_Table goodsInfo) {

        return goodsInfoMapper.update_goods(goodsInfo);

    }


    //管理更新商品信息
    public int update_goods_admin(GoodsInfo_Table goodsInfo_table) {
        return goodsInfoMapper.update_goods_admin(goodsInfo_table);
    }
    //管理首页显示
    public int update_show_admin(int goods_id){
        return goodsInfoMapper.update_show_admin(goods_id);
    }


    //查看单个商品详情
    public GoodsInfo findOneById_goods(int goods_id) {
        System.out.println("impl"+goods_id);
        GoodsInfo goods = goodsInfoMapper.findOneById_goods(goods_id);
        System.out.println(goods.toString());
        return goods;
    }

    //获得所有商品
    public List<GoodsInfo> findAll_goods() {
        List<GoodsInfo> lists = goodsInfoMapper.findAll_goods();
        System.out.println("共有商品："+lists.size());
        return lists;
    }

    public List<GoodsInfo> findShow_goods() {
        List<GoodsInfo> lists = goodsInfoMapper.findShow_goods();
        return lists;
    }

    //获得某个类别的商品
    public List<GoodsInfo> findType_goods(int typeid) {
        System.out.println(typeid+"===implement");
        List<GoodsInfo> lists = goodsInfoMapper.findType_goods(typeid);
        System.out.println(typeid+"类商品数量："+lists.size());
        return lists;
    }

    /**
     * 新旧程度
     * @param
     * @return
     */

    public List<OldLevel> findAllLevel() {
        List<OldLevel> lists = goodsInfoMapper.findAllLevel();
        return lists;
    }

    /**
     * 个人中心：商品发布
     * @param userid
     * @return
     */

    public List<GoodsInfo> findPublish_goods(int userid){
        List<GoodsInfo> lists = goodsInfoMapper.findPublish_goods(userid);
        return lists;
    }

    /**
     * 个人中心：商品收藏
     * @param userid
     * @return
     */

    public int add_coll(int userid, int goods_id) {
        return goodsCollectionMapper.add(userid,goods_id);
    }

    public int del_coll(int coll_id) {

        return goodsCollectionMapper.del(coll_id);
    }

    public List<GoodsCollection> findByUserId_coll(int userid) {
        List<GoodsCollection> list = goodsCollectionMapper.findByUserId(userid);
        return list;
    }
}
